class Solution(object):
    def minSwaps(self, nums):
        n = len(nums)
        k = nums.count(1)
        if k == n:
            return 0
        t = sum(nums[:k])
        max_val = t
        for i in range(n - k):
            if nums[i]:
                t -= 1
            if nums[i + k]:
                t += 1
                max_val = max(max_val, t)
        min_val = k - max_val
        k = n - k
        t = sum(nums[:k])
        min_val = min(min_val, t)
        for i in range(n - k):
            if nums[i]:
                t -= 1
            if nums[i + k]:
                t += 1
            min_val = min(min_val, t)
        return min_val


data = Solution()
nums = [1, 1, 0, 0, 1]
print(data.minSwaps(nums))
